package top.infopub.mgr.mkt.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.mkt.dao.ProSellCoopDao;
import top.infopub.mgr.mkt.dao.ProSellCostDao;
import top.infopub.mgr.mkt.dao.ProSellDao;
import top.infopub.mgr.mkt.dao.ProSellSubDao;
import top.infopub.mgr.mkt.domain.ProSellDomain;
import top.infopub.mgr.mkt.service.ProSellService;
import top.infopub.mgr.mkt.vo.ProSellVo;
import top.infopub.model.mkt.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class ProSellServiceImpl extends GenericServiceImpl<ProSell, String> implements
		ProSellService {

	@Autowired
	private ProSellDao proSellDao;

	@Autowired
    private ProSellCoopDao proSellCoopDao;

    @Autowired
    private ProSellSubDao proSellSubDao;

    @Autowired
    private ProSellCostDao proSellCostDao;

    @Autowired
    private WsFlowProcessService wsFlowProcess;

	@Override
	public GenericDao<ProSell, String> getDao() {
		return proSellDao;
	}

    @Override
    public Page<ProjectInfo> selectPageByDomainWhere(Page<ProjectInfo> page,
                                                       ProSellVo proSellVo) throws ParseException {
        //处理时间范围
        if (!StringUtils.isEmpty(proSellVo.getDates())) {
            String[] splitTime = proSellVo.getDates().split("~");
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
                proSellVo.setEndDate(format.parse(splitTime[1]));
                proSellVo.setStartDate(format.parse(splitTime[0]));
        }else { //因为前端可能会传递空字符串的日期类型 所以在这里制空
            proSellVo.setDates(null);
        }
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ProjectInfo>)proSellDao.selectByDomainWhere(proSellVo);
    }

    @Override
    public Page<ProSellDomain> selectMyCheckByDomainWhere(Page<ProSellDomain> page,
                                                          ProSellVo proSellVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ProSellDomain>)proSellDao.selectMyCheckByDomainWhere(proSellVo);
    }

    @Override
    public ProSellVo selectById(String id) {
        return proSellDao.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addProSell(ProSell proSell, List<ProSellCoop> proSellCoops,
                           List<ProSellSub> proSellSubs, List<ProSellCost> proSellCosts) {
        //插入预立项记录
        proSellDao.insertById(proSell);

        //批量插入联合体单位
        if(CollectionUtils.isNotEmpty(proSellCoops)){
            proSellCoopDao.insertByBatch(proSellCoops);
        }

        //批量插入子项
        if(CollectionUtils.isNotEmpty(proSellSubs)){
            proSellSubDao.insertByBatch(proSellSubs);
        }

        //批量营销费用
        if(CollectionUtils.isNotEmpty(proSellCosts)){
            proSellCostDao.insertByBatch(proSellCosts);
        }

        // 注意 这里根据不同的业务分支判断条件 获得不同enum
        FlowTemplateEnum templateEnum = FlowTemplateEnum.MKT_PRO_SELL;
        // 处理工作流数据
        wsFlowProcess.launch(templateEnum, proSell.getId(), proSell.getProcessId(), null);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editProSell(ProSell proSell, List<ProSellCost> proSellCosts,String submitType) {
        //更新
        proSellDao.updateByIdSelective(proSell);
        proSellCostDao.deleteByProSellId(proSell.getId());
        //批量营销费用
        if(CollectionUtils.isNotEmpty(proSellCosts)){
            proSellCostDao.insertByBatch(proSellCosts);
        }

        if("2".equalsIgnoreCase(submitType)){
            proSell=proSellDao.selectById(proSell.getId());
            ProSell sell=new ProSell();
            sell.setId(proSell.getId());
            sell.setProcessStatus("1");//审判中
            proSellDao.updateByIdSelective(sell);
            // 注意 这里根据不同的业务分支判断条件 获得不同enum
            FlowTemplateEnum templateEnum = FlowTemplateEnum.MKT_PRO_SELL;
            // 处理工作流数据
            wsFlowProcess.launch(templateEnum, proSell.getId(), proSell.getProcessId(), null);

        }


    }
}
